package com.beifu.manager.api.controller.baseInfo;

import com.beifu.manager.api.common.PageInfo;
import com.beifu.manager.api.domain.baseInfo.Organize;
import com.beifu.manager.api.domain.baseInfo.OrganizeListResponse;
import com.beifu.manager.api.domain.feedback.FeedBackInfo;
import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.dao.OrganizerMapper;
import com.beifu.manager.dao.TrainInfoMapper;
import com.beifu.manager.model.AssetType;
import com.beifu.manager.model.Organizer;
import com.beifu.manager.model.TrainInfo;
import com.beifu.manager.service.assetinfo.AssetInfoService;
import com.utils.ResponseHelp;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: licy
 * @Date :  2018/7/5 10:17
 * @Last Modified by: licy
 * @Last Modified time:  2018/7/5 10:17
 */
@RestController
@RequestMapping(value = "/mobileserve/baseInfo/api", produces = "application/json;charset=UTF-8")
//@RequestMapping(value="/baseInfo/api", produces = "application/json;charset=UTF-8")
public class BaseInfoApiController {

    private Logger Log = Logger.getLogger(this.getClass());

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private OrganizerMapper organizerMapper;

    @Autowired
    private AssetTypeMapper assetTypeMapper;

    @Autowired
    private AssetInfoService assetInfoService;

    /**
     * 获取火车信息列表
     *
     * @param reqMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getTrainInfo", method = RequestMethod.POST)
    public String getTrainInfo(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
//        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }

        List<TrainInfo> trainInfos = trainInfoMapper.selectByExample(null);

        return ResponseHelp.responseApiSuccessText(trainInfos);
    }

    /**
     * 获取火车信息列表
     *
     * @param reqMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOrginzeInfo", method = RequestMethod.POST)
    public String getOrginzeInfo(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
//        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }

        List<Organizer> organizerList = organizerMapper.selectByExample(null);

        return ResponseHelp.responseApiSuccessText(organizerList);
    }

    /**
     * 获取型号基本信息
     */
    @ResponseBody
    @RequestMapping(value = "/getAssetType", method = RequestMethod.POST)
    public String getAssetType(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
//        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }

        List<AssetType> assetTypeList = assetTypeMapper.selectByExample(null);

        return ResponseHelp.responseApiSuccessText(assetTypeList);
    }

    /**
     * 获取型号基本信息
     */
    @ResponseBody
    @RequestMapping(value = "/saveBaseRfid", method = RequestMethod.POST)
    public String saveBaseRfid(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }


//        List<AssetType> assetTypeList = assetTypeMapper.selectByExample(null);
        try {
            assetInfoService.saveBaseRfidList(jsonIn);
        } catch (RuntimeException e) {
            String message = e.getMessage();
            return ResponseHelp.responseApiErrorText("400021", message);
        } catch (Exception e) {
            return ResponseHelp.responseApiErrorText("400022", "请求出错请联系管理员");
        }


        return ResponseHelp.responseApiSuccessText("ok");
    }

    /**
     * 获取组织信息
     */
    @ResponseBody
    @RequestMapping(value="/getOrganizeList", method= RequestMethod.POST)
    public String getOrganizeList(@RequestBody Map<String, Object> reqMap) {
        // 接收pub和in区域的请求数据
        JSONObject jsonPub = JSONObject.fromObject(reqMap.get("pub"));
//        JSONObject jsonIn = JSONObject.fromObject(reqMap.get("in"));

        String userId = "";
        String devID = "";
        String timestamp = "";
        if (!JSONUtils.isNull(jsonPub)) {
            userId = (String) jsonPub.get("userId");
            devID = (String) jsonPub.get("devID");
            timestamp = (String) jsonPub.get("timestamp");
        }

        List<Organizer> records = organizerMapper.selectByExample(null);

        if (records == null || records.size() <= 0) {
            return ResponseHelp.responseApiErrorText("00404","组织信息不存在");
        }
        // 定义返回
        List<OrganizeListResponse> response = new ArrayList<OrganizeListResponse>();
        OrganizeListResponse organizeListResponse1 = new OrganizeListResponse();
        OrganizeListResponse organizeListResponse2 = new OrganizeListResponse();
        OrganizeListResponse organizeListResponse3 = new OrganizeListResponse();
        OrganizeListResponse organizeListResponse4 = new OrganizeListResponse();
        List<Organize> organizeList1 = new ArrayList<Organize>();
        List<Organize> organizeList2 = new ArrayList<Organize>();
        List<Organize> organizeList3 = new ArrayList<Organize>();
        List<Organize> organizeList4 = new ArrayList<Organize>();

        for (Organizer organizer : records) {
            if (organizer.getOrganizeType() == 0) {
                Organize organize = new Organize();
                organize.setOrganizeId(organizer.getId().toString());
                organize.setOrganizeName(organizer.getOrganizeName());
                organizeListResponse1.setTypeId(organizer.getOrganizeType().toString());
                organizeListResponse1.setTypeName("火车");
                organizeList1.add(organize);
                organizeListResponse1.setOrganizeList(organizeList1);
            } else if (organizer.getOrganizeType() == 1) {
                Organize organize = new Organize();
                organize.setOrganizeId(organizer.getId().toString());
                organize.setOrganizeName(organizer.getOrganizeName());
                organizeListResponse2.setTypeId(organizer.getOrganizeType().toString());
                organizeListResponse2.setTypeName("段");
                organizeList2.add(organize);
                organizeListResponse2.setOrganizeList(organizeList2);
            } else if (organizer.getOrganizeType() == 2) {
                Organize organize = new Organize();
                organize.setOrganizeId(organizer.getId().toString());
                organize.setOrganizeName(organizer.getOrganizeName());
                organizeListResponse3.setTypeId(organizer.getOrganizeType().toString());
                organizeListResponse3.setTypeName("洗涤厂");
                organizeList3.add(organize);
                organizeListResponse3.setOrganizeList(organizeList3);
            } else if (organizer.getOrganizeType() == 3) {
                Organize organize = new Organize();
                organize.setOrganizeName(organizer.getOrganizeName());
                organize.setOrganizeId(organizer.getId().toString());
                organizeListResponse4.setTypeId(organizer.getOrganizeType().toString());
                organizeListResponse4.setTypeName("库");
                organizeList4.add(organize);
                organizeListResponse4.setOrganizeList(organizeList4);
            } else {
                // 不匹配的类型。doNothing
                Log.error("================api接口=查询组织类型，不匹配的类型，请查看==================");
            }
        }
        response.add(organizeListResponse1);
        response.add(organizeListResponse2);
        response.add(organizeListResponse3);
        response.add(organizeListResponse4);
        return ResponseHelp.responseApiSuccessText(response);
    }
}